/*
    This file is part of FNote.

    FNote is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    FNote is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with FNote.  If not, see <http://www.gnu.org/licenses/>.

    Copyright (c) 2009, Maciej "Fazi" Gałkowski <fazi.dev@gmail.com>
*/

#include <QtGui>
#include <QDir>
#include <QFile>

#include "mainwindow.h"

// constructor
// confDir is a path to valid config directory
MainWindow::MainWindow(const QString &configDirStr): appSettings(NULL)
{
    // find (or create) config file in specified directory - or die
    QDir settingsDir(configDirStr);
    qDebug() << settingsDir;

    QString settingsFile = QDir::cleanPath(settingsDir.absoluteFilePath("config.ini"));

    appSettings = new QSettings(settingsFile, QSettings::IniFormat);
    if(!appSettings->isWritable())
    {
        delete appSettings;
        throw QObject::tr("Settings file \"%1\" cannot be accessed, aborting.").arg(settingsFile);
    }

    loadSettings();

    // detect&setup clipboard mode
    const QClipboard *clipboard = QApplication::clipboard();
    clipboardMode = QClipboard::Clipboard;
    if(clipboard->supportsSelection())
    {
        clipboardMode = QClipboard::Selection;
    }
    qDebug() << "Clipboard mode: " << clipboardMode;

    createTrayIcon();

    connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayActivated(QSystemTrayIcon::ActivationReason)));
}

// ignore close events, just hide window
void MainWindow::closeEvent(QCloseEvent *event)
{
    hide();
    event->ignore();
}

// process tray event
void MainWindow::trayActivated(QSystemTrayIcon::ActivationReason reason)
{
    switch(reason)
    {
        case QSystemTrayIcon::Trigger:
        case QSystemTrayIcon::DoubleClick:
            qDebug() << "Tray clicked";
            toggleWindow();
            break;
        case QSystemTrayIcon::MiddleClick:
            qDebug() << "Tray middle-clicked";
            fastPaste();
            break;
        default:
            break;
    }
}

// toggle main window
void MainWindow::toggleWindow()
{
    qDebug() << "toggleWindow() called";
    setVisible(!isVisible());
}

// Fast paste may not work on win systems
void MainWindow::fastPaste()
{
    qDebug() << "fastPaste() called";
    const QClipboard *clipboard = QApplication::clipboard();
    const QMimeData *mimeData = clipboard->mimeData(clipboardMode);

    if(mimeData->hasText())
    {
        trayIcon->showMessage(tr("FNote"), tr("Fast paste: ")+mimeData->text());
    }
    else
    {
        trayIcon->showMessage(tr("FNote"), tr("No text in clipboard."), QSystemTrayIcon::Warning);
    }
}

// create tray icon and menu
void MainWindow::createTrayIcon()
{
    QAction *quitAction = new QAction(tr("&Quit"), this);
    quitAction->setShortcut(tr("Ctrl+Q"));
    quitAction->setIcon(QIcon(":/images/exit.png"));
    connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));

    QMenu *trayIconMenu = new QMenu(this);
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(quitAction);

    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setContextMenu(trayIconMenu);
    trayIcon->setIcon(QIcon(":/images/fnote.svg"));
    trayIcon->show();
}

MainWindow::~MainWindow()
{
    saveSettings();
    delete appSettings;
}

void MainWindow::loadSettings()
{
    qDebug() << "Loading settings";

    QPoint pos = appSettings->value("main/pos", QPoint(200, 200)).toPoint();
    QSize size = appSettings->value("main/size", QSize(400, 400)).toSize();

    resize(size);
    move(pos);

    qDebug() << "Settings loaded";
}

void MainWindow::saveSettings()
{
    qDebug() << "Saving settings";

    appSettings->setValue("main/pos", pos());
    appSettings->setValue("main/size", size());

    qDebug() << "Settings saved";
}